package org.model;


public class NetAddress {
	private int port;
	private byte[] ipAddress;
	public NetAddress(int port, byte[] ipAddress) {
		super();
		this.port = port;
		this.ipAddress = ipAddress;
	}
	public NetAddress(Integer cport, char[] address) {
		ipAddress = new byte[4];
		for (int i = 0; i < address.length; i++) {
			ipAddress[i] = (byte) address[i];
		}
		port = cport;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public byte[] getIpAddress() {
		return ipAddress;
	}
	public void setIpAddress(byte[] ipAddress) {
		this.ipAddress = ipAddress;
	}
	
	@Override
	public boolean equals(Object obj) {
		NetAddress cObj = (NetAddress) obj;
		if(compareBytes( cObj.ipAddress, ipAddress) && cObj.port == port )
			return true;
		return false;
	}
	
	@Override
	public int hashCode() {
		int hashCode = 0;
		for (int i = 0; i < ipAddress.length; i++) {
			hashCode += ipAddress[i];
		}
		hashCode += port;
		return hashCode;
	}
	private boolean compareBytes(byte[] x,byte[] y)
	{
		if(x.length != y.length)
			return false;
		for (int i = 0; i < y.length; i++) {
			if(x[i] != y[i])
			    return false;
		}
		return true;
	}
}
